package com.kaili.controller.web.v1;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.common.exception.BaseExcept;
import com.kaili.db.service.IHsOptEwbService;
import com.kaili.except.ResponseStatus;
import com.kaili.pojo.dto.report.DailyDeliveryReportDTO;
import com.kaili.pojo.dto.report.ServiceReportDTO;
import com.kaili.pojo.vo.DailyDeliveryEwbVO;
import com.kaili.pojo.vo.DailyDeliveryReportVO;
import com.kaili.pojo.vo.DailyDeliveryStatisticsVO;
import com.kaili.pojo.vo.ServiceReportVO;
import com.kaili.version.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanMap;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xjh
 * @ClassName ReportController
 * @Description 报表相关
 * @Date 上午9:42 18-6-8
 * @Version 1.0
 */
@ApiVersion(1)
@RestController
@RequestMapping("/{api_version}")
@Api(tags = "报表相关服务", description = "报表相关API")
public class ReportController {
    private static final Logger logger = LogManager.getLogger();

    @Autowired
    private IHsOptEwbService ewbService;

    @PostMapping("/dailyDeliveryReports")
    @ApiOperation(value = "日发货量报表")
    DailyDeliveryReportVO dailyDeliveryReport(@RequestBody DailyDeliveryReportDTO dailyDeliveryReportDTO) {
        Map<Object, Object> map = new HashMap<>();
        map.putAll(new BeanMap(dailyDeliveryReportDTO));
        try{
            List<DailyDeliveryStatisticsVO> statisticsVOList = ewbService.queryStatistics(map);
            Page<DailyDeliveryEwbVO> ewbPager = new Page<DailyDeliveryEwbVO>(dailyDeliveryReportDTO.getCur(), dailyDeliveryReportDTO.getPageSize());
            ewbPager = ewbService.queryDailyDeliveryEwbList(ewbPager, map);
            DailyDeliveryReportVO dailyDeliveryReportVO  = new DailyDeliveryReportVO();
            dailyDeliveryReportVO.setStatisticsVOList(statisticsVOList);
            dailyDeliveryReportVO.setEwbPager(ewbPager);
            return dailyDeliveryReportVO;
        }catch (Exception e){
            e.printStackTrace();
            logger.info(e);
            throw new BaseExcept(ResponseStatus.DAILY_DELIVERY_REPORT_QUERY_ERR);
        }

    }

    @PostMapping("/serviceReports")
    @ApiOperation(value = "经营业务报表")
    Page<ServiceReportVO> queryEwbList(@RequestBody ServiceReportDTO serviceReportDTO) throws BaseExcept {
        if (serviceReportDTO.getCur() != null && serviceReportDTO.getPageSize() != null) {
            Page<ServiceReportVO> pager = new Page<ServiceReportVO>(serviceReportDTO.getCur(), serviceReportDTO.getPageSize());
            Map<Object, Object> map = new HashMap<>();
            map.putAll(new BeanMap(serviceReportDTO));
            pager = ewbService.queryServiceStatistics(pager, map);
            return pager;
        }
        throw new BaseExcept(ResponseStatus.BUSINESS_OPERATION_ANALYSIS);
    }
}
